package com.jf3q.front.controller;

import com.jf3q.common.domain.EduOrder;
import com.jf3q.common.vo.ResultVo;
import com.jf3q.front.service.EduOrderService;
import com.jf3q.front.service.TradeService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

@RestController
@RequestMapping("/eduorder")
public class OrderController {

    //支付宝当面付2.0服务
    @Autowired
    private TradeService tradeService;


    @Autowired
    EduOrderService orderService;
    //生成订单
    @PostMapping("/order/createOrder")
    public ResultVo createOrder(@RequestBody EduOrder eduOrder){
        String outTradeNo = String.valueOf((int)((Math.random()*9+1)*100000000));
        eduOrder.setOrderNo(outTradeNo);
        eduOrder.setEduCreate(new Date());
        eduOrder.setEduModified(new Date());
        eduOrder.setIsDeleted((byte) 0);
        orderService.save(eduOrder);
        return ResultVo.success("",eduOrder.getId());
    }

    @GetMapping("/order/getOrderInfo/{id}")
    public ResultVo getOrderInfo(@PathVariable String id){
        EduOrder byId = orderService.getById(id);
        return ResultVo.success("根据订单id查询订单信息",byId);
    }

    //创建支付二维码
    @GetMapping("/paylog/createNative/{orderNo}")
    public ResultVo getQrCode(@PathVariable String orderNo){
        EduOrder eduOrder = tradeService.tradeQrCode(orderNo);
        if (eduOrder!=null){
            return ResultVo.success("",eduOrder);
        }
        return ResultVo.error("");
    }

    //查询订单情况
    @GetMapping("/paylog/queryPayStatus/{orderNo}")
    public ResultVo queryOrderStatus(@PathVariable String orderNo){
        boolean result = tradeService.alipayTradeQuery(orderNo);
        return ResultVo.success("",result);
    }


//    @PostMapping("/payCallback")
//    public void payCallback(){
//        Map<String,String> map=new HashMap<>();
//        Enumeration<String> parameterNames = getRequest().getParameterNames();
//        while (parameterNames.hasMoreElements()){
//            String parameter = parameterNames.nextElement();
//            if (!parameter.toLowerCase().equals("sign_type")){
//                map.put(parameter,getRequest().getParameter(parameter));
//            }
//        }
//        try {
//            boolean result = AlipaySignature.rsaCertCheckV2(map, Configs.getPublicKey(), "utf-8", Configs.getSignType());
//            PrintWriter writer = getResponse().getWriter();
//            if (result){
//                writer.print("success");
//            }else {
//                writer.print("unSuccess");
//            }
//        } catch (AlipayApiException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (AlipayApiException e) {
//            throw new RuntimeException(e);
//        }
//    }

//    @GetMapping("/paylog/createNative/{orderNo}")
//    public String Native(@PathVariable Integer orderNo){
//    }

}
